<?php
/**
 * TaskController
 * 任务处理
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: TaskController.php 678 2011-07-19 06:39:42Z maomao.thx@gmail.com $
 */
class TaskController extends IG_Controller_Action
{
    /**
     * 最大等待时间，
     * @var unknown_type
     */
    private $max_wait = 300;

    public function indexAction()
    {
        set_time_limit(120);
        $type = $this->_request->getParam('type', 'system');
        $method = $type . 'Action';
        if (method_exists($this, $method)) {
            $this->$method();
        }
    }

    public function systemAction()
    {
        $this->disableLayout();
        $this->setRender();

        $lockfile = APPLICATION_PATH . '/../data/cache/task_lock_system';

        $mtime = 'microtime:'.time();
        if (!file_exists($lockfile)) {
            file_put_contents($lockfile, $mtime );
        } else {
            $lockcontent = file_get_contents($lockfile);
            $difftime = time() - substr($lockcontent, 10);

            if($difftime >= 120){
                unlink($lockfile);
            }
            echo '任务执行中，稍后继续执行';
            exit();
        }


        $task = new System_Model_Task();
        $taskList = $task->fetchAll(array('status=?' => 0), 'task_id', 500);
        if (0 < ($count = count($taskList))) {
            $maxId = $taskList[$count-1]->getTaskId();
            $task->updateStatus($maxId, 1);
        }
        foreach ($taskList as $t)
        {
            try {
                $handler = IG_Task::factory($t->getType(), $t);
                $result = $handler->process();
                if ($result) {
                    $t->delete();
                }
                //echo '系统任务 ' . $t->getTaskId() . ' 已处理完成！<br />';
            } catch (IG_Task_Exception $te) {
                $t->setStatus(2)
                    ->save();
                echo '系统任务 ' . $t->getTaskId() . ' 处理失败：' . $te->getMessage() . '<br />';
                IG_Log::log($te->getMessage(), Zend_Log::ERR);
            } catch (Exception $e) {
                echo '系统任务 ' . $t->getTaskId() . ' 处理失败：' . $e->getMessage() . '<br />';
                IG_Log::log($e->getMessage(), Zend_Log::ERR);
            }
        }
        if (isset($maxId)) {
            $task->updateStatus($maxId, 0);
        }

        unlink($lockfile);
        echo 'task done';
    }

    public function gameAction()
    {
        $this->disableLayout();
        $this->setRender();

        $lockfile = APPLICATION_PATH . '/../data/cache/task_lock_game';

        $mtime = 'microtime:'.time();
        if (!file_exists($lockfile)) {
            file_put_contents($lockfile, $mtime );
        } else {
            $lockcontent = file_get_contents($lockfile);
            $difftime = time() - substr($lockcontent, 10);

            if($difftime >= 120){
                unlink($lockfile);
            }
            echo '任务执行中，稍后继续执行';
            exit();
        }

        $task = new Game_Model_Task();
        $taskList = $task->fetchAll(array('status=?' => 0), 'task_id', 2500);
        if (0 < ($count = count($taskList))) {
            $minId = $taskList[0]->getTaskId();
            $maxId = $taskList[$count-1]->getTaskId();
            $task->updateStatus($minId, $maxId, 1);
        }
        foreach ($taskList as $t)
        {
            try {
                $handler = IG_Task::factory($t->getType(), $t);
                $result = $handler->process();
                if ($result) {
                    $t->delete();
                }
                //echo '游戏任务 ' . $t->getTaskId() . ' 已处理完成！<br />';
            } catch (IG_Task_Exception $te) {
                $t->setStatus(2)
                    ->save();
                echo '游戏任务 ' . $t->getTaskId() . ' 处理失败：' . $te->getMessage() . '<br />';
                IG_Log::log($te->getMessage(), Zend_Log::ERR);
            } catch (Exception $e) {
                echo '游戏任务 ' . $t->getTaskId() . ' 处理失败：' . $e->getMessage() . '<br />';
                IG_Log::log($e->getMessage(), Zend_Log::ERR);
            }
        }
        if (isset($minId)) {
            $task->updateStatus($minId, $maxId, 0);
        }


        unlink($lockfile);
        echo 'task done';
    }
}